/**
 * 聊天室 二进制服务端
 */

const  log = require("./log");

const Net = require("net");

const util = require('./util');

// 创建服务的
const Server = Net.createServer();


// 获取客户端连接句柄
Server.on('connection', (socket) => {
    log.print_r('from ' + socket.remoteAddress);

    // 获取客户端的buf
    socket.on('data', (buf) => {
        
        util.skGet(socket, buf, (msg) => {
            console.log(msg);
        })
        
        setTimeout(() => {
            socket.write(util.skSend("dlc"))
        }, 1000);
    });

    socket.on('close', () => {
        log.print_r('from ' + socket.remoteAddress + ' close');
    });

    socket.on('error', (error) => {
        error.from = socket.remoteAddress;
        log.print_r(error);
    });

});

Server.on('error', (error) => {
    log.print_r(error);
});

// 服务器监控打开
Server.on('listening', () => {
    log.print_r('server  listening');
});

// 开启服务监控
Server.listen({
    host: '0.0.0.0', //任何端都可以连接
    port: 9999,
});


